package crazy.reflect;

import java.io.FileInputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

//根据配置文件来创建对象   Spring采用XML配置文件
public class ObjectPoolFactory
{
	private Map<String, Object> objectPool = new HashMap<String, Object>();
	
	private Object createObject(String clazzName) throws Exception
	{
		Class<?> clazz = Class.forName(clazzName);
		return clazz.newInstance();
 	}
	
	public void initPool(String fileName) throws Exception
	{
		FileInputStream fis = null;
		try
		{
			fis = new FileInputStream(fileName);
			Properties props = new Properties();
			props.load(fis);
			for (String name : props.stringPropertyNames())
			{
				objectPool.put(name, createObject(props.getProperty(name)));
			}
		}
		catch (Exception e)
		{
			// TODO: handle exception
		}
		finally
		{
			try
			{
				if (fis != null)
				{
					fis.close();
				}
			}
			catch (Exception e2)
			{
				// TODO: handle exception
			}
		}
	}
	
	public Object getObject(String name)
	{
		return objectPool.get(name);
		
	}
	
	public static void main(String[] args) throws Exception
	{
		ObjectPoolFactory pf = new ObjectPoolFactory();
		pf.initPool("D:\\workspace\\Algorithm\\src\\crazy\\reflect\\obj.txt");
		System.out.println(pf.getObject("a"));
	}
}
